'use strict';
const BaseController = require('./base');
const serviceName = 'idleItem';

class IdleItemController extends BaseController {
  constructor(ctx) {
    super(ctx, serviceName);
  }

  getPageQuery(query) {
    const where = {
    };
    const { is_public, name, type, category_name,category_id, province, city, condition, status, create_user_id } = query;

    if (name) {
      where.name = {
        [this.app.Sequelize.Op.like]: `%${name}%`,
      };
    } else {
      if (type) {
        where.type = type;
      }
    }
    if (is_public) {
      where.is_public = is_public
    }
    if (category_name) {
      where.category_name = {
        [this.app.Sequelize.Op.like]: `%${category_name}%`,
      }
    }
    if (province) {
      where.province = province;
    }
    if (city) {
      where.city = city;
    }
    if (category_id) {
      where.category_id = category_id
    }
    if (condition) {
      where.condition = condition;
    }
    if (status) {
      where.status = status;
    }
    // 数量大于0

    if (create_user_id) {
      where.create_user_id = create_user_id
    } else {
      where.quantity = {
        [this.app.Sequelize.Op.gt]: 0
      }
    }
    console.log(where)
    return where;
  }
  async updateIdleItemStatus() {
    console.log('000---')
    if (!this.ctx.request.body.id) {
      throw new Error('id not found');
    }
    await this.ctx.service[this.serviceName].updateIdleItemStatus(this.ctx.request.body.id)
    this.ctx.success(true)
  }
  async nearby() {
    const { ctx } = this;
    const { longitude, latitude, distance = 50 } = ctx.query;
    if (!longitude || !latitude) {
      ctx.throw(400, '经纬度参数不能为空');
    }
    const result = await ctx.service[this.serviceName].getNearby(longitude, latitude, distance, ctx.query);
    ctx.success(result);
  }

  async incrementViewCount() {
    const { ctx } = this;
    const { id } = ctx.params;
    if (!id) {
      ctx.throw(400, 'ID参数不能为空');
    }
    await ctx.service[this.serviceName].incrementViewCount(id);
    ctx.success(id);
  }

  async batchImport() {
    const { ctx } = this;

    // 支持两种方式：文件上传和JSON数据
    if (ctx.request.files && ctx.request.files.length > 0) {
      // 文件上传方式
      const file = ctx.request.files[0];
      if (!file) {
        ctx.throw(400, '请上传Excel文件');
      }
      const result = await ctx.service[this.serviceName].batchImportFile(file);
      ctx.success(result);
    } else {
      // JSON数据方式
      const { items } = ctx.request.body;
      if (!items || !Array.isArray(items) || items.length === 0) {
        ctx.throw(400, '请提供有效的物资/设备数据');
      }
      const result = await ctx.service[this.serviceName].batchImportJson(items);
      ctx.success(result);
    }
  }
  async bathDelete(){
    const { ctx } = this;
    const { idList } = ctx.request.body;
    if (!idList || !Array.isArray(idList) || idList.length === 0) {
      ctx.throw(400, '请提供有效的物资/设备数据');
    }
    const result = await ctx.service[this.serviceName].bathDelete(idList);
    ctx.success(result);
  }
  async getExcelTemplate() {
    const { ctx } = this;
    const result = await ctx.service[this.serviceName].getExcelTemplate();
    ctx.set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    ctx.set('Content-Disposition', 'attachment; filename=idle_item_template.xlsx');
    ctx.body = result;
  }

  async statistics() {
    const { ctx } = this;
    const { startDate, endDate, type } = ctx.query;
    const result = await ctx.service[this.serviceName].getStatistics(startDate, endDate, type);
    ctx.success(result);
  }

  async myItems() {
    const { ctx } = this;
    const userId = ctx.state.user.id;
    const result = await ctx.service[this.serviceName].getMyItems(userId, ctx.query);
    ctx.success(result);
  }

  async adminPage() {
    const { query, service } = this.ctx;
    let { pageNum, pageSize } = query;
    let page = Number(pageNum) || 1;
    let size = Number(pageSize) || 10;
    delete query.pageNum
    delete query.pageSize
    let where = {
      ...this.getPageQuery(query)
    }
    delete where.is_public
    const list = await service[this.serviceName].getPage(where, page, size);
    this.ctx.success(list);
  }

  /**
   *
   *   router.get('/api/idleItem/userStats', controller.idleItem.getUserDeviceStats);
   *   router.get('/api/idleItem/systemStats', controller.idleItem.getSystemDeviceStats);
   *   router.get('/api/idleTransfer/stats', controller.idleItem.getTransferStats);
   */
  async getUserDeviceStats() {
    const { ctx } = this;
    const userId = ctx.query.user_id
    const type = ctx.query.type
    const result = await ctx.service[this.serviceName].getUserDeviceStats(userId, type);
    ctx.success(result);
  }

  async getSystemDeviceStats() {
    const { ctx } = this;
    const type = ctx.query.type
    const result = await ctx.service[this.serviceName].getSystemDeviceStats(type);
    ctx.success(result);
  }
  async getTransferStats() {
    const { ctx } = this;
    let { startDate, endDate, type } = ctx.query
    const result = await ctx.service[this.serviceName].getTransferStats(startDate, endDate, type);
    ctx.success(result);
  }
}

module.exports = IdleItemController;
